package com.henu.acmrating.crawler;

import com.henu.acmrating.pojo.User;
import com.henu.acmrating.pojo.UserOj;
import com.henu.acmrating.service.UserOjService;
import com.henu.acmrating.service.UserService;
import com.henu.acmrating.webmagic.SeleniumDownloader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class CFScoreProcessor implements PageProcessor {

    private static final String OJ_NAME = "Codeforces";
    private static final String BASE_URL = "https://codeforces.com/profile/";

    public static String getUrl(String id) {
        return BASE_URL + id;
    }

    @Resource
    UserOjService userOjService;

    @Resource
    UserService userService;

    public static CFScoreProcessor cfScoreProcessor;

    @PostConstruct
    public void init() {
        cfScoreProcessor = this;
    }

    @Override
    public void process(Page page) {
        String account = page.getUrl().toString().split("/")[4];
        Html html = page.getHtml();
        String score = html.xpath("//div[@class=info]/ul/li[1]/span[1]/allText()").toString();
        if(score == null) return ;
        UserOj userOj = cfScoreProcessor.userOjService.selectByAccountAndOjName(account, OJ_NAME);
        User user = new User();
        user.setUserId(userOj.getUserId());
        user.setUserCfScore(Integer.valueOf(score));
        cfScoreProcessor.userService.updateByPrimaryKeySelective(user);
    }

    private Site site = Site.me()
            .setTimeOut(30 * 1000)  // 设置超时时间
            .setRetrySleepTime(3000)// 设置重试的间隔时间
            .setRetryTimes(3);      // 设置重试次数

    @Override
    public Site getSite() {
        return site;
    }


    @Value("${prop.driver-path}")
    private String driverPath;

    @Scheduled(cron = "0 0 9  ? * * ")
    public void start() {
        Spider.create(new CFScoreProcessor())
                .setDownloader(new SeleniumDownloader(driverPath))
                .addUrl(BaseProcessor.getUrls(userOjService, OJ_NAME, BASE_URL))
                .thread(1)
                .start();
    }


    public static void run_all() {
        Spider.create(new CFScoreProcessor())
                .setDownloader(new SeleniumDownloader(cfScoreProcessor.driverPath))
                .addUrl(BaseProcessor.getUrls(cfScoreProcessor.userOjService, OJ_NAME, BASE_URL))
                .thread(1)
                .start();
    }



    public static void run(String id) {
        Spider.create(new CFScoreProcessor())
                .setDownloader(new SeleniumDownloader(cfScoreProcessor.driverPath))
                .addUrl(BASE_URL + id)
                .thread(1)
                .start();
    }

    public static void main(String[] args) {
        Spider.create(new CFScoreProcessor())
                .setDownloader(new SeleniumDownloader("D:/Environment/chromedriver.exe"))
                .addUrl(BASE_URL + "henu_16l_lxl")
                .thread(1)
                .start();
    }
}